package com.baidu.mapapi.demo.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.demo.FrameParam;
import com.baidu.mapapi.demo.R;

public class BMap_GeoCoder extends MapActivity implements OnClickListener {
	Button mBtnReverseGeoCode = null; // 将坐标反编码为地址
	Button mBtnGeoCode = null; // 将地址编码为坐标

	MapView mMapView = null; // 地图View
	MKSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.bmap_geocoder);
		
		FrameParam app = (FrameParam) this.getApplication();
		if (app.mBMapManager == null) {
			app.mBMapManager = new BMapManager(getApplication());
			app.mBMapManager.init(app.mapKey, new FrameParam.MyGeneralListener());
		}
		app.mBMapManager.start();
		super.initMapActivity(app.mBMapManager);

		initUI();

		// 初始化搜索模块，注册事件监听
		mSearch = new MKSearch();
		mSearch.init(app.mBMapManager, new MKSearchListener() {
			public void onGetAddrResult(MKAddrInfo res, int error) {
				if (error != 0) {
					String str = String.format("错误号：%d", error);
					Toast.makeText(BMap_GeoCoder.this, str, Toast.LENGTH_LONG)
							.show();
					return;
				}

				mMapView.getController().animateTo(res.geoPt);

				String strInfo = String.format("纬度：%f 经度：%f\r\n",
						res.geoPt.getLatitudeE6() / 1e6,
						res.geoPt.getLongitudeE6() / 1e6);

				Toast.makeText(BMap_GeoCoder.this, strInfo, Toast.LENGTH_LONG)
						.show();
				Drawable marker = getResources().getDrawable(R.drawable.bmap_point_green); // 得到需要标在地图上的资源
				marker.setBounds(0, 0, marker.getIntrinsicWidth(),
						marker.getIntrinsicHeight()); // 为maker定义位置和边界
				mMapView.getOverlays().clear();
				mMapView.getOverlays().add(
						new MyItemizedOverlay(marker, BMap_GeoCoder.this,
								res.geoPt, res.strAddr));
			}

			public void onGetPoiResult(MKPoiResult res, int type, int error) {
				if (error != 0 || res == null) {
					Toast.makeText(BMap_GeoCoder.this, "解析失败",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (res != null && res.getCurrentNumPois() > 0) {
					GeoPoint ptGeo = res.getAllPoi().get(0).pt;
					// 移动地图到该点：
					mMapView.getController().animateTo(ptGeo);

					String strInfo = String.format("纬度：%f 经度：%f\r\n",
							ptGeo.getLatitudeE6() / 1e6,
							ptGeo.getLongitudeE6() / 1e6);
					strInfo += "\r\n附近有：";
					for (int i = 0; i < res.getAllPoi().size(); i++) {
						strInfo += (res.getAllPoi().get(i).name + ";");
					}
					Toast.makeText(BMap_GeoCoder.this, strInfo,
							Toast.LENGTH_LONG).show();
				}
			}

			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
			}

			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onGetRGCShareUrlResult(String arg0, int arg1) {
				// TODO Auto-generated method stub

			}

		});

	}

	public void initUI() {

		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);

		mBtnReverseGeoCode = (Button) findViewById(R.id.reversegeocode);
		mBtnGeoCode = (Button) findViewById(R.id.geocode);
		mBtnReverseGeoCode.setOnClickListener(this);
		mBtnGeoCode.setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		FrameParam app = (FrameParam) this.getApplication();
		app.mBMapManager.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		FrameParam app = (FrameParam) this.getApplication();
		app.mBMapManager.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();

		public MyItemizedOverlay(Drawable marker, Context context, GeoPoint pt,
				String title) {
			super(boundCenterBottom(marker));

			mGeoList.add(new OverlayItem(pt, title, null));

			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mGeoList.get(i);
		}

		@Override
		public int size() {
			return mGeoList.size();
		}

		@Override
		public boolean onSnapToItem(int i, int j, Point point, MapView mapview) {
			Log.e("ItemizedOverlayDemo", "enter onSnapToItem()!");
			return false;
		}
	}

	@Override
	public void onClick(View v) {

		if (mBtnReverseGeoCode.equals(v)) {
			GeoPoint ptCenter = new GeoPoint(39904965, 116327764);// mMapView.getMapCenter();
			mSearch.reverseGeocode(ptCenter);
		} else if (mBtnGeoCode.equals(v)) {
			EditText editCity = (EditText) findViewById(R.id.city);
			EditText editGeoCodeKey = (EditText) findViewById(R.id.geocodekey);
			mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
					.getText().toString());
		}

	}

}
